/**************************
* @Summary
* @Author 20009200303 赵传博
* @Date 6/7/2022
* @Note
*****************************/

#ifndef EX6_MINUSNODE_H
#define EX6_MINUSNODE_H
#include "BinaryOpNode.h"

class MinusNode : public BinaryOpNode{
    ExprNode::ExprNode(Token_type tokType, ExprNode *left, ExprNode *right, double value) {
        this->tokType = tokType;
        this->left = left;
        this->right = right;
        this->value = value;
    }
// 1 * 2 + 3
    double ExprNode::getValue() const {
        return value;
    }

    double ExprNode::calculate() {
        stack<double> Stack;
        ExprNode::getText_postfix();
        for(int i = 0; i < txt.size(); ++i){
            if(txt.at(i) == ' ') continue;
            if(i == 0 || (isdigit(txt.at(i)) && !isdigit(txt.at(i - 1)) && txt.at(i - 1) != '.')){
                //stod用于将string转成double
                Stack.push(stod(txt.substr(i, txt.find_first_of(' ', i) - i)));
            }else if(txt.at(i) == '+'){
                double d = Stack.top(); Stack.pop();
                d += Stack.top(); Stack.pop();
                Stack.push(d);
            }else if(txt.at(i) == '-'){
                double d2 = Stack.top(); Stack.pop();
                double d1 = Stack.top(); Stack.pop();
                d1 -= d2;
                Stack.push(d1);
            }else if(txt.at(i) == '*'){
                double d = Stack.top(); Stack.pop();
                d *= Stack.top(); Stack.pop();
                Stack.push(d);
            }else if(txt.at(i) == '/'){
                double d2 = Stack.top(); Stack.pop();
                double d1 = Stack.top(); Stack.pop();
                d1 /= d2;
                Stack.push(d1);
            }
        }
        txt.clear();
        return Stack.top();
    }

    void ExprNode::getText_mid() {
        txt.append("(");
        if(this->getLeft()) this->getLeft()->getText_mid();
        switch (tokType) {
            case '+': txt.append("+");break;
            case '-': txt.append("-");break;
            case '*': txt.append("*");break;
            case '/': txt.append("/");break;
            case NUMBER: case NAME:{
                //这里判断是否是整数，如果是，去掉小数点后面的0
                string val_str;
                if((int)value != value){
                    val_str = to_string(value);
                    val_str = val_str.substr(0, val_str.find_last_not_of('0') + 1);
                }else{
                    val_str = to_string((int)value);
                }
                txt.append(val_str);
                break;
            }
//        case NAME:{
//            //这里的操作同上
//            string name_val;
//            if((int)getValue() != getValue()){
//                name_val = to_string(getValue());
//                name_val = name_val.substr(0, name_val.find_last_not_of('0') + 1);
//            }else{
//                name_val = to_string((int)getValue())
//            }
//            txt.append(name_val);
//            break;
//        }
            case ASSIGNEND:{
                txt.append("Assigned successfully");
                break;
            }
            default:;
        }
        if(this->getRight()) this->getRight()->getText_mid();
        txt.append(")");
    }

    void ExprNode::getText_postfix() {
        if(this->getLeft()) this->getLeft()->getText_postfix();
        if(this->getRight()) this->getRight()->getText_postfix();
        switch (tokType) {
            case '+': txt.append("+ ");break;
            case '-': txt.append("- ");break;
            case '*': txt.append("* ");break;
            case '/': txt.append("/ ");break;
            case NUMBER: case NAME:{
                //这里判断是否是整数，如果是，去掉小数点后面的0
                string val_str;
                if((int)value != value){
                    val_str = to_string(value);
                    val_str = val_str.substr(0, val_str.find_last_not_of('0') + 1);
                }else{
                    val_str = to_string((int)value);
                }
                txt.append(val_str + " ");
                break;
            }
            case ASSIGNEND:{
                txt.append("Assigned successfully");
                break;
            }
            default:;
        }
    }

    void ExprNode::getText_prefix() {
        switch (tokType) {
            case '+': txt.append("+ ");break;
            case '-': txt.append("- ");break;
            case '*': txt.append("* ");break;
            case '/': txt.append("/ ");break;
            case NUMBER: case NAME:{
                //这里判断是否是整数，如果是，去掉小数点后面的0
                string val_str;
                if((int)value != value){
                    val_str = to_string(value);
                    val_str = val_str.substr(0, val_str.find_last_not_of('0') + 1);
                }else{
                    val_str = to_string((int)value);
                }
                txt.append(val_str + " ");
                break;
            }
            case ASSIGNEND:{
                txt.append("Assigned successfully");
                break;
            }
            default:;
        }
        if(this->getLeft()) this->getLeft()->getText_prefix();
        if(this->getRight()) this->getRight()->getText_prefix();
    }

    Token_type ExprNode::getTokType() const {
        return tokType;
    }

    void ExprNode::setAssignEnd() {
        tokType = ASSIGNEND;
    }



    ExprNode::~ExprNode() = default;

};


#endif //EX6_MINUSNODE_H
